package com.qding.park.manager.timer.schedule.job;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;

import com.qding.park.common.core.constant.CacheKeyParent;
import com.qding.park.common.core.constant.CacheKeyTwo;
import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.lock.JedisLock;
import com.qding.park.manager.timer.ParkTimerApplication;
import com.qding.park.manager.timer.TimerContext;
import com.qding.park.manager.timer.conf.TaskConfig;

/**
 * @ClassName: AbstractJob
 * @Description: TODO(抽象job 父类)
 * @author Administrator
 * @date 2016年9月14日 throws JobExecutionException
 */
public abstract class AbstractJob implements Job {

	private static final Log _log = LogFactory.getLog(AbstractJob.class);
	
	@Override
	public void execute(JobExecutionContext job) throws JobExecutionException {

		IExceptionHandle exceptionHandle = ParkTimerApplication.ctxUtil.getApplicationContext()
				.getBean(IExceptionHandle.class);
		// 得到redis client
		JedisClient jedisClient = ParkTimerApplication.ctxUtil.getApplicationContext().getBean(JedisClient.class);
		String lockName = CacheKeyTwo.LOCK_KEY + CacheKeyParent.SEPARATOR + job.getJobDetail().getKey();
		JedisLock lock = new JedisLock(jedisClient, lockName);
			try {
				if(lock.acquire()){
					// 得到锁
					execJob(job);
				}
				
			} catch (Exception e) {
				exceptionHandle.handle(e);
				lock.release();
			} finally {
				lock.release();
			}
	}

	protected abstract void execJob(JobExecutionContext job);
	
	
	protected void  reloadTask(String jobName,String corTime) throws SchedulerException {
		if(StringUtils.isBlank(jobName)){
			return;
		}
		TimerContext  timerContext=ParkTimerApplication.ctxUtil.getApplicationContext().getBean(TimerContext.class);
		Scheduler sched = timerContext.getSched();
        TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TimerContext.taskGroupName);
        CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);  
        
        if (trigger == null) {
        	_log.warn("没有此定时器");
            return;  
        }  
        
        String oldTime = trigger.getCronExpression(); 
		String newTime=corTime.trim();
		
		if(StringUtils.isBlank(newTime)||newTime.equals(oldTime)){
			_log.warn("oldTime:"+oldTime+" newTime:"+newTime);
			return ;
		}
		
		 /** 方式一 ：调用 rescheduleJob 开始 */
        // 触发器  
        TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
        // 触发器名,触发器组  
        triggerBuilder.withIdentity(jobName, TimerContext.taskGroupName);
        triggerBuilder.startNow();
        // 触发器时间设定  
        triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(newTime));
        // 创建Trigger对象
        trigger = (CronTrigger) triggerBuilder.build();
        // 方式一 ：修改一个任务的触发时间
        sched.rescheduleJob(triggerKey, trigger);
	}

}
